package com.kele.controller;

import com.kele.domain.Blog;
import com.kele.domain.Comment;
import com.kele.domain.User;
import com.kele.domain.pageInfo;
import com.kele.service.BlogService;
import com.kele.service.CommentService;
import com.kele.service.CommentSonService;
import com.kele.service.UserService;
import com.kele.utils.PageUtil;
import com.kele.utils.ResponseData;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.ListIterator;

/**
 * @author 栗守佳
 */
@Controller
@RequestMapping("/comment")
public class CommentController {
    @Resource
    CommentService commentService;
    @Resource
    BlogService blogService;
    @Resource
    UserService userService;
    @Resource
    CommentSonService commentSonService;


    /**
     * 无参遍历评论,用于后台管理
     * @author 栗守佳
     * @param model
     * @param pageNum
     * @param dateNums
     * @param pageBarSize
     * @return
     */
    @RequestMapping(value = "/findComments", method = RequestMethod.GET)
    public String findComments(Model model,
                               @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                               @RequestParam(value = "dateNums", defaultValue = "5") int dateNums,
                               @RequestParam(value = "pageBarSize", defaultValue = "5") int pageBarSize,
                               @RequestParam(value = "flag", defaultValue = "-1") int flag) {
        int count = userService.findAllSize("comment", flag);
        PageUtil pageUtil = new PageUtil(pageNum, count, dateNums);
        pageNum = pageUtil.getPageNum();
        int start = pageUtil.getStart();
        List<Comment> comments = commentService.findComments(start, dateNums);
        pageInfo pageInfo = new pageInfo(comments, pageNum, count, dateNums, pageBarSize);
        model.addAttribute("pageInfo", pageInfo);
        return "static_resources/templates/management";
    }

    /**
     * 用户进行评论
     *
     * @author 栗守佳
     * @param comment
     * @param request
     * @return
     */
    @RequestMapping(value = "addComment", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData addComment(Comment comment, HttpServletRequest request) {
//        User user= (User) request.getSession().getAttribute("User");
//        comment.setBlogger(user.getNickname());
        if (commentService.addComment(comment) > 0) {
            return ResponseData.success("发布成功", comment);
        } else {
            return ResponseData.fail("发布失败");
        }

    }

    /**
     * 遍历某文章下的评论
     * @author 栗守佳
     * @param blogid
     * @param id
     * @return
     */
    @RequestMapping(value = "/findComment", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData findComment(int blogid,int id) {
        Blog blog = null;
        blog = blogService.queryById(blogid, id);
        System.out.println(blog);
        List<Comment> commentList = commentService.findComment(blog.getBid());
        System.out.println(commentList.size());

        return ResponseData.success("查询成功", commentList);
    }

    /**
     * 用户删除评论,子评论也会连带删除
     * @author 栗守佳
     * @param commentid
     * @return
     */
    @RequestMapping(value = "/deleteComment", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData deleteComment(int commentid) {
        int count = commentService.deleteComment(commentid);
        //删除主评论顺带删除子评论
        commentSonService.deleteCommentSons(commentid);
        return ResponseData.success("主评论删除成功", count);
    }
}




